package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models/trans/common"
	servicecommon "jlpay-sdk-go/pkg/jlpay/services/trans/common"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 构建退款请求
func buildRefundRequest() *common.RefundRequest {
	request := common.NewRefundRequest()

	// 设置请求参数
	request.SetMchID("849584358120018")
	request.SetOutTradeNo("20250804200002zyfTest")
	request.SetOriOutTradeNo("20250312100002zyfTest")
	//request.SetOriTransactionID("451137612050898301747226")
	request.SetTotalFee("1")
	request.SetRefundFee("50")
	request.SetRemark("测试退款")
	request.SetMchCreateIP("127.0.0.1")
	request.SetLongitude("116.397128")
	request.SetLatitude("39.916527")

	return request
}

// 退款示例
func refundDemo(refundService *servicecommon.RefundService) {
	// 创建退款请求
	request := buildRefundRequest()

	// 序列化请求数据并打印
	requestMap, _ := request.ToMap()
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("退款请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := refundService.Refund(request)
	if err != nil {
		utils.Errorf("退款请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("退款")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 退款示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和退款服务
	config := demo.CreateDemoConfig()
	refundService := servicecommon.NewRefundService(config)

	refundDemo(refundService)

	utils.Info("=============================")
	utils.Info("===== 退款示例执行完毕 =====")
	utils.Info("=============================")
}
